import axios from 'axios';
import {push} from 'react-router-redux';
import {Spin,Icon} from 'antd';
export function GetData(pagination){
    return (dispatch)=>{
        dispatch({type:'get_load'});
        return axios.get('http://101.132.191.91:6011/api/user?pageindex='+pagination.current+'&pagesize='+pagination.pageSize)
                .then((response)=>{
                let data = response.data;
                if(typeof(data.Message)=='string'){
                    data=JSON.parse(data.Message);
                }else{
                    data=data.Message;
                }
                pagination.total=data.dataCount;
                pagination.current =data.pageIndex;
                pagination.pageSize =10;
                let d=[];
                let list=data.list;
                for(let i=0;i<list.length;i++){
                     d.push({
                        Id: list[i].ID,
                        UserId: list[i].UserId,
                        UserName: list[i].UserName,
                        UserGender:list[i].UserGender,
                        UserTelPhone: list[i].UserTelPhone
                    });
                }
                dispatch({type:'init_Data',data:d,pagination:pagination});

            });
    }
}
export function DeleteItem(key){
    return (dispatch)=>{
        dispatch({type:'get_load'});
        return del('http://101.132.191.91:6011/api/user/'+key,{}).then((data)=>{
             dispatch({type:'load_item'});
             if(typeof(data)=='string'){
                    data=JSON.parse(data);
                }else{
                    data=data;
                }
                if(data.State == 1){
                    let pagination={};
                    pagination.current = 1,
                    pagination.pageSize = 10,
                    dispatch(GetData(pagination));
                    console.log("删除成功");
                }else{
                    console.log(data.Message);
                }
               
        })
    }
}
export function AddData(form){
    return (dispatch)=>{
          dispatch({type:'get_load'});
        return post('http://101.132.191.91:6011/api/user',{
            UserId:form.id,
            UserName:form.username,
            UserGender:form.sex,
            UserTelPhone:form.telephone
        }).then((data)=>{
           dispatch({type:'load_item'});
            if(typeof(data)=='string'){
                data=JSON.parse(data);
            }else{
                data=data;
            }
            if(data.State == 1){
                console.log('添加成功');
                let pagination={};
                pagination.current=1,
                pagination.pageSize=10,
                dispatch(GetData(pagination));
            }else{
                console.log('添加失败');
            }
        })
    }
}
export function EditData(form,id){
    return (dispatch)=>{
         dispatch({type:'get_load'});
      return  put('http://101.132.191.91:6011/api/user/'+id,{
            UserId:form.id,
            UserName:form.username,
            UserGender:form.sex,
            UserTelPhone:form.telephone
        }).then((data)=>{
            dispatch({type:'load_item'});
            if(typeof(data)=='string'){
                data=JSON.parse(data);
            }else{
                data=data;
            }
            if(data.State == 1){
                let pagination={};
                pagination.current = 1,
                pagination.pageSize = 10,
                dispatch(GetData(pagination));
                console.log('修改成功');
            }else{
                console.log(data.Message);
            }
        })
    }
}